package com.g2room.mq.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class KafkaReceiver {

    @KafkaListener(topics = {"${kafka.topic}"})
    public void listen(ConsumerRecord<?, ?> record) {
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            System.out.println("----------------- record =" + record);
            System.out.println("------------------ message =" + message);
            /**
             接收到消息如下：
             record =ConsumerRecord(topic = Caeri2Ev-call, partition = 1, leaderEpoch = 2, offset = 0, CreateTime = 1594263176263, serialized key size = -1, serialized value size = 333, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = {"eventData":{"collisionType":1,"ecallEventCode":"2004261650150001","eventAddr":"重庆市渝北区礼学路","eventLat":29.673428,"eventLng":106.475286,"eventTime":"2020-04-26 16:50:15","eventTriggerType":1,"flInjuryRate":2.5},"ownerData":{"ownerName":"张三","ownerPhone":"15310660622"},"vehicleData":{"vehiclePlate":"渝A4PU21"}})
             message ={"eventData":{"collisionType":1,"ecallEventCode":"2004261650150001","eventAddr":"重庆市渝北区礼学路","eventLat":29.673428,"eventLng":106.475286,"eventTime":"2020-04-26 16:50:15","eventTriggerType":1,"flInjuryRate":2.5},"ownerData":{"ownerName":"张三","ownerPhone":"15310660622"},"vehicleData":{"vehiclePlate":"渝A4PU21"}}
             record =ConsumerRecord(topic = Caeri2Ev-call, partition = 0, leaderEpoch = 3, offset = 1, CreateTime = 1594263176269, serialized key size = -1, serialized value size = 228, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = {"eventData":{"collisionType":1,"ecallEventCode":"2004261650150001","eventAddr":"重庆市渝北区礼学路","eventLat":29.673428,"eventLng":106.475286,"eventTime":"2020-04-26 16:50:15","eventTriggerType":1,"flInjuryRate":2.5}})
             message ={"eventData":{"collisionType":1,"ecallEventCode":"2004261650150001","eventAddr":"重庆市渝北区礼学路","eventLat":29.673428,"eventLng":106.475286,"eventTime":"2020-04-26 16:50:15","eventTriggerType":1,"flInjuryRate":2.5}}
             */
        }
    }
}